﻿using DataAccess;
using DevExpress.XtraEditors;
using QuanLyKhachSan.Validation;
using System;
using System.Linq;
using System.Windows.Forms;
using NLog;

namespace QuanLyKhachSan
{
    public partial class frmThietBiPhong : XtraForm
    {
        ThietBiPhongManager controller = new ThietBiPhongManager();
        protected static Logger _logger = LogManager.GetCurrentClassLogger();
        public ThietBiPhong tbp = null;
        bool them = false;
        public bool themthanhcong = false;
        bool capnhat;
        public frmThietBiPhong()
        {
            InitializeComponent();
        }

        public frmThietBiPhong(bool them)
        {
            InitializeComponent();
            this.them = them;
        }

        public frmThietBiPhong(ThietBiPhong tbp)
        {
            InitializeComponent();
            capnhat = !them;
            this.tbp = tbp;
        }

        private void frmThietBiPhong_Load(object sender, EventArgs e)
        {
            this.loaiPhongTableAdapter.Fill(this.qLKHACHSANDataSet.LoaiPhong);
            Loading();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            Loading();
        }

        private void Loading()
        {
            if (them)
            {
                txtDonVi.Text = "";
                txtTenThietBi.Text = "";
                mtxtThietBi.Text = "";
                nudSoLuong.Value = 0;
                chkThietBi.Checked = false;
                cbMaLoaiPhong.SelectedIndex = -1;
            }
            else
            {
                txtDonVi.Text = tbp.DonVi;
                txtTenThietBi.Text = tbp.TenTB;
                mtxtThietBi.Text = tbp.MaTB;
                nudSoLuong.Value = tbp.SoLuong;
                chkThietBi.Checked = tbp.ThietBiCoDinh;
                cbMaLoaiPhong.SelectedValue = tbp.MaLoaiPhong;
            }
            
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (them)
            {
                ThemThietBi();
            }
            else
            {
                mtxtThietBi.Enabled = false;
                CapNhatThietBi();
            }
        }

        private void ThemThietBi()
        {
            tbp = new ThietBiPhong();
            tbp.MaTB = mtxtThietBi.Text;
            tbp.TenTB = txtTenThietBi.Text;
            tbp.SoLuong = (int)nudSoLuong.Value;
            tbp.ThietBiCoDinh = chkThietBi.Checked;
            tbp.MaLoaiPhong = cbMaLoaiPhong.SelectedValue + "";
            tbp.DonVi = txtDonVi.Text;
            ValidateResult vResult = controller.Validate(tbp);
            if (!vResult.Message.Any())
            {
                ActionResult aResult = controller.Them(tbp);
                if (!aResult.Exceptions.Any())
                {
                    MessageBox.Show("Thêm thành công thiết bị ID= " + tbp.MaTB);
                    themthanhcong = true;
                    _logger.Info("Thêm thành công thiết bị ID= " + tbp.MaTB);
                    this.Close();
                }
                else
                {
                    string s = "";
                    foreach (var ex in aResult.Exceptions)
                    {
                        s += ex.Message + "\n";
                    }
                    MessageBox.Show(s);
                    _logger.Error(s);
                }
            }
            else
            {
                string s = "";
                foreach (var ex in vResult.Message)
                {
                    s += ex + "\n";
                }
                MessageBox.Show(s);
                _logger.Error(s);
            }
        }
        private void CapNhatThietBi()
        {
            tbp = new ThietBiPhong();
            mtxtThietBi.Enabled = false;
            tbp.MaTB = mtxtThietBi.Text;
            tbp.TenTB = txtTenThietBi.Text;
            tbp.SoLuong = (int)nudSoLuong.Value;
            tbp.ThietBiCoDinh = chkThietBi.Checked;
            tbp.MaLoaiPhong = cbMaLoaiPhong.SelectedValue + "";
            tbp.DonVi = txtDonVi.Text;
            ValidateResult vResult = controller.Validate(tbp);
            if (!vResult.Message.Any())
            {
                ActionResult aResult = controller.CapNhat(tbp);
                if (!aResult.Exceptions.Any())
                {
                    MessageBox.Show("Cập nhật thành công thiết bị ID= " + tbp.MaTB);
                    themthanhcong = true;
                    _logger.Info("Cập nhật thành công thiết bị ID= " + tbp.MaTB);
                    this.Close();
                }
                else
                {
                    string s = "";
                    foreach (var ex in aResult.Exceptions)
                    {
                        s += ex.Message + "\n";
                    }
                    MessageBox.Show(s);
                    _logger.Error(s);
                }
            }
            else
            {
                string s = "";
                foreach (var ex in vResult.Message)
                {
                    s += ex + "\n";
                }
                MessageBox.Show(s);
                _logger.Error(s);
            }
        }
    }
}
